#!/bin/bash

set -e

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

log_debug() {
    echo -e "${BLUE}[DEBUG]${NC} $1"
}

# 检查Docker是否运行
check_docker() {
    log_info "Checking Docker status..."
    if ! docker info > /dev/null 2>&1; then
        log_error "Docker is not running or not accessible"
        exit 1
    fi
    log_info "Docker is running"
}

# 检查Docker Compose是否可用
check_docker_compose() {
    log_info "Checking Docker Compose..."
    if ! docker-compose --version > /dev/null 2>&1; then
        log_error "Docker Compose is not available"
        exit 1
    fi
    log_info "Docker Compose is available"
}

# 构建前端镜像
build_frontend() {
    log_info "Building frontend Docker image..."
    docker build -t task-center-front:latest .
    log_info "Frontend image built successfully"
}

# 启动所有服务
start_services() {
    log_info "Starting all services..."
    docker-compose up -d
    
    log_info "Waiting for services to start..."
    sleep 10
    
    log_info "Checking service status..."
    docker-compose ps
}

# 显示服务信息
show_service_info() {
    log_info "Service URLs:"
    echo "  Frontend: http://localhost:8081"
    echo "  Backend API: http://localhost:8080"
    echo "  Nginx: http://localhost:80"
    echo "  PhpMyAdmin: http://localhost:8899"
    echo "  Redis Admin: http://localhost:8082"
    echo "  MySQL: localhost:3306"
    echo "  Redis: localhost:16379"
    echo "  Kafka: localhost:9092"
}

# 检查服务健康状态
check_health() {
    log_info "Checking service health..."
    
    # 检查前端
    if curl -f -s http://localhost:8081/health > /dev/null 2>&1; then
        log_info "Frontend is healthy"
    else
        log_warn "Frontend health check failed"
    fi
    
    # 检查后端
    if curl -f -s http://localhost:8080/actuator/health > /dev/null 2>&1; then
        log_info "Backend is healthy"
    else
        log_warn "Backend health check failed"
    fi
}

# 停止所有服务
stop_services() {
    log_info "Stopping all services..."
    docker-compose down
    log_info "All services stopped"
}

# 查看日志
show_logs() {
    local service=${1:-""}
    if [ -n "$service" ]; then
        log_info "Showing logs for $service..."
        docker-compose logs -f "$service"
    else
        log_info "Showing all logs..."
        docker-compose logs -f
    fi
}

# 清理所有
cleanup() {
    log_info "Cleaning up all services and images..."
    docker-compose down --volumes --remove-orphans
    docker rmi task-center-front:latest 2>/dev/null || true
    log_info "Cleanup completed"
}

# 显示帮助
show_help() {
    echo "Usage: $0 [COMMAND]"
    echo ""
    echo "Commands:"
    echo "  start     Build frontend and start all services"
    echo "  stop      Stop all services"
    echo "  restart   Restart all services"
    echo "  logs      Show logs (optionally specify service name)"
    echo "  status    Show service status"
    echo "  health    Check service health"
    echo "  cleanup   Stop and remove all services and images"
    echo "  help      Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 start           # Build and start all services"
    echo "  $0 logs            # Show all logs"
    echo "  $0 logs task-center-front  # Show frontend logs"
    echo "  $0 cleanup         # Clean everything"
}

# 主函数
main() {
    case "${1:-start}" in
        start)
            check_docker
            check_docker_compose
            build_frontend
            start_services
            show_service_info
            check_health
            ;;
        stop)
            stop_services
            ;;
        restart)
            stop_services
            sleep 2
            start_services
            show_service_info
            ;;
        logs)
            show_logs "$2"
            ;;
        status)
            docker-compose ps
            ;;
        health)
            check_health
            ;;
        cleanup)
            cleanup
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            log_error "Unknown command: $1"
            show_help
            exit 1
            ;;
    esac
}

# 运行主函数
main "$@" 